﻿Imports System.Configuration
Imports log4net
Imports System.IO
Imports System.Data
Imports System.Data.Sql
Imports System.Data.SqlClient


Public Class ConexionBD
    Private strConnection As String
    Private strEsquema As String
    Private strServer As String
    Private strDatabase As String
    Private strUID As String
    Private strPassword As String
    Private strSecurity As String
    Private strPath As String = My.Application.Info.DirectoryPath
    Private oDataset As DataSet
    Private Shared ReadOnly log As ILog = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)
    Private miCnx As SqlConnection

    Public Sub New()
        Me.cargaDataXML()
        log.Debug("Conexion " + strConnection)
        log.Debug("Esquema " + strEsquema)
    End Sub

    Private Sub cargaDataXML()
        Dim oFileInfo As FileInfo
        Dim asds As String = strPath & "\Conexion.xml"
        oFileInfo = New FileInfo(strPath & "\Conexion.xml")
        If oFileInfo.Exists Then
            oDataset = New DataSet
            oDataset.ReadXml(strPath & "\Conexion.xml")
            Me.strServer = oDataset.Tables("data").Rows(0).Item("server")
            Me.strDatabase = oDataset.Tables("data").Rows(0).Item("database")
            Me.strUID = oDataset.Tables("data").Rows(0).Item("uid")
            Me.strPassword = oDataset.Tables("data").Rows(0).Item("password")
            Me.strSecurity = oDataset.Tables("data").Rows(0).Item("security")
            'Me.strConnection = "server=" + Me.strServer + ";uid=" + Me.strUID + ";password=" + Me.strPassword + ";database=" + Me.strDatabase
            Console.WriteLine(Me.strServer)
            Console.WriteLine(Me.strDatabase)
            Console.WriteLine(Me.strUID)
            Console.WriteLine(Me.strPassword)
            'Me.strConnection = "Data Source=" + Me.strServer + ";Initial Catalog=" + Me.strDatabase + ";User Id=" + Me.strUID + ";Password=" + Me.strPassword + ";"
            Me.strConnection = "Data Source=" + Me.strServer + ";Initial Catalog=" + Me.strDatabase + ";User Id=" + Me.strUID + ";Password=" + Me.strPassword + ";Integrated Security=" + Me.strSecurity + ";" 'Trufa's
            'Me.strConnection = "Data Source=VALENTINA\SQLEXPRESS;Initial Catalog=master;Integrated Security=SSPI"
            'Me.strConnection = "Data Source=CAMILA\LOCAL;Initial Catalog=prueba;Integrated Security=SSPI;"

            'Me.strEsquema = oDataset.Tables("data").Rows(0).Item("schema")
        End If
    End Sub

    Property EsquemaBD() As String
        Get
            Return strEsquema
        End Get
        Set(ByVal value As String)
            strEsquema = value
        End Set
    End Property
    Property CadenaConexion() As String
        Get
            Return strConnection
        End Get
        Set(ByVal value As String)
            strConnection = value
        End Set
    End Property
    Property Servidor() As String
        Get
            Return strServer
        End Get
        Set(ByVal value As String)
            strServer = value
        End Set
    End Property
    Property BaseDeDatos() As String
        Get
            Return strDatabase
        End Get
        Set(ByVal value As String)
            strDatabase = value
        End Set
    End Property
    Property User() As String
        Get
            Return strUID
        End Get
        Set(ByVal value As String)
            strUID = value
        End Set
    End Property
    Property Pass() As String
        Get
            Return strPassword
        End Get
        Set(ByVal value As String)
            strPassword = value
        End Set
    End Property
    Private cadenaCnx As String

    Public Function obtenerCnx() As SqlConnection
        Try
            miCnx = New SqlConnection(Me.strConnection)
            miCnx.Open()
            Return miCnx
        Catch ex As Exception
            MsgBox("Error en conexión", MsgBoxStyle.Critical, "Error")
            Return miCnx
            Exit Function
        End Try
    End Function

    Public Sub cerrarCnx()
        miCnx.Close()
        'hola amor tqm
    End Sub

End Class
